package com.zenithmind.coding.pojo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Objects;

/**
 * 代码质量分析实体类 - 遵循单一职责原则，专门处理代码质量分析信息
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@TableName("coding_code_quality_analysis")
@Schema(description = "代码质量分析实体")
public class CodeQualityAnalysis extends BaseEntity {

    /**
     * 提交ID
     */
    @Schema(description = "提交ID")
    @EqualsAndHashCode.Include
    private String submissionId;

    /**
     * 代码质量评分
     */
    @Schema(description = "代码质量评分")
    private Double qualityScore;

    /**
     * 代码复杂度
     */
    @Schema(description = "代码复杂度")
    private Integer complexity;

    /**
     * 圈复杂度
     */
    @Schema(description = "圈复杂度")
    private Integer cyclomaticComplexity;

    /**
     * 有效代码行数
     */
    @Schema(description = "有效代码行数")
    private Integer effectiveLineCount;

    /**
     * 注释行数
     */
    @Schema(description = "注释行数")
    private Integer commentLineCount;

    /**
     * 空白行数
     */
    @Schema(description = "空白行数")
    private Integer blankLineCount;

    /**
     * 代码风格评分
     */
    @Schema(description = "代码风格评分")
    private Integer styleScore;

    /**
     * 可读性评分
     */
    @Schema(description = "可读性评分")
    private Integer readabilityScore;

    /**
     * 可维护性评分
     */
    @Schema(description = "可维护性评分")
    private Integer maintainabilityScore;

    /**
     * 性能评分
     */
    @Schema(description = "性能评分")
    private Integer performanceScore;

    /**
     * 安全性评分
     */
    @Schema(description = "安全性评分")
    private Integer securityScore;

    /**
     * 代码重复度
     */
    @Schema(description = "代码重复度")
    private Double duplicationRate;

    /**
     * 函数数量
     */
    @Schema(description = "函数数量")
    private Integer functionCount;

    /**
     * 类数量
     */
    @Schema(description = "类数量")
    private Integer classCount;

    /**
     * 变量数量
     */
    @Schema(description = "变量数量")
    private Integer variableCount;

    /**
     * 最大函数长度
     */
    @Schema(description = "最大函数长度")
    private Integer maxFunctionLength;

    /**
     * 平均函数长度
     */
    @Schema(description = "平均函数长度")
    private Double avgFunctionLength;

    /**
     * 最大嵌套深度
     */
    @Schema(description = "最大嵌套深度")
    private Integer maxNestingDepth;

    /**
     * 代码建议（JSON格式）
     */
    @Schema(description = "代码建议（JSON格式）")
    private String suggestions;

    /**
     * 代码问题（JSON格式）
     */
    @Schema(description = "代码问题（JSON格式）")
    private String issues;

    /**
     * 代码优化建议（JSON格式）
     */
    @Schema(description = "代码优化建议（JSON格式）")
    private String optimizations;

    /**
     * 分析引擎版本
     */
    @Schema(description = "分析引擎版本")
    private String engineVersion;

    /**
     * 分析规则集
     */
    @Schema(description = "分析规则集")
    private String ruleSet;

    /**
     * 重写equals方法以确保里氏替换原则
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        if (!super.equals(obj)) return false;
        
        CodeQualityAnalysis that = (CodeQualityAnalysis) obj;
        return Objects.equals(submissionId, that.submissionId);
    }

    /**
     * 重写hashCode方法以确保里氏替换原则
     */
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), submissionId);
    }

    /**
     * 计算综合质量评分
     */
    public Double calculateOverallScore() {
        if (styleScore == null || readabilityScore == null || 
            maintainabilityScore == null || performanceScore == null || 
            securityScore == null) {
            return qualityScore;
        }
        
        // 权重：风格20%，可读性25%，可维护性25%，性能15%，安全性15%
        return (styleScore * 0.2 + readabilityScore * 0.25 + 
                maintainabilityScore * 0.25 + performanceScore * 0.15 + 
                securityScore * 0.15);
    }

    /**
     * 计算代码密度
     */
    public Double calculateCodeDensity() {
        if (effectiveLineCount == null || commentLineCount == null || blankLineCount == null) {
            return null;
        }
        
        int totalLines = effectiveLineCount + commentLineCount + blankLineCount;
        if (totalLines == 0) {
            return 0.0;
        }
        
        return (double) effectiveLineCount / totalLines * 100;
    }

    /**
     * 计算注释率
     */
    public Double calculateCommentRate() {
        if (effectiveLineCount == null || commentLineCount == null) {
            return null;
        }
        
        if (effectiveLineCount == 0) {
            return 0.0;
        }
        
        return (double) commentLineCount / effectiveLineCount * 100;
    }

    /**
     * 检查代码质量等级
     */
    public String getQualityLevel() {
        if (qualityScore == null) {
            return "未知";
        }
        
        if (qualityScore >= 90) {
            return "优秀";
        } else if (qualityScore >= 80) {
            return "良好";
        } else if (qualityScore >= 70) {
            return "中等";
        } else if (qualityScore >= 60) {
            return "及格";
        } else {
            return "需要改进";
        }
    }
}
